package com.practice.springboot_01_zwj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.ProjectReport;
import com.practice.springboot_01_zwj.mapper.ProjectReportMapper;
import com.practice.springboot_01_zwj.service.ProjectReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProjectReportServiceImpl implements ProjectReportService {
    @Autowired
    private ProjectReportMapper dao;

    @Override
    public int addProjectReport(ProjectReport projectReport) {
        // 验证项目时间逻辑
        validateProjectDates(projectReport);
        return dao.addProjectReport(projectReport);
    }
    /**
     * 验证项目时间
     * @param projectReport 项目报告
     * @throws IllegalArgumentException 如果时间验证失败
     */
    private void validateProjectDates(ProjectReport projectReport) {
        if (projectReport.getProjectBeginTime() != null && projectReport.getProjectEndTime() != null) {
            // 检查项目结束时间是否早于开始时间
            if (projectReport.getProjectEndTime().before(projectReport.getProjectBeginTime())) {
                throw new IllegalArgumentException("项目结束时间不能早于项目开始时间");
            }
        }
    }

    @Override
    public int updateProjectReport(ProjectReport projectReport) {
        // 计算时间进度和落实情况
        calculateProgressAndImplementation(projectReport);

        return dao.updateProjectReport(projectReport);
    }

    @Override
    public int deleteProjectReport(Integer keyId) {
        return dao.deleteProjectReport(keyId);
    }

    @Override
    public PageInfo<ProjectReport> selectProjectReport(String projectName, String projectType, int pageNum, int pageSize) {
        // 1. 参数校验
        if (pageNum < 1) pageNum = 1;
        if (pageSize < 1 || pageSize > 100) pageSize = 10;

        // 2. 确保在查询前启动分页（这是关键步骤）
        PageHelper.startPage(pageNum, pageSize);

        // 3. 处理空字符串情况
        if (projectName != null && projectName.isEmpty()) {
            projectName = null;
        }
        if (projectType != null && projectType.isEmpty()) {
            projectType = null;
        }

        // 4. 执行查询（必须在 startPage 之后立即调用）
        List<ProjectReport> networks = dao.selectProjectReport(projectName, projectType);

        // 5. 使用 PageInfo 包装结果
        return new PageInfo<>(networks);
      }

    @Override
    public Integer createProjectReport(ProjectReport projectReport) {
        // 验证项目时间逻辑
        validateProjectDates(projectReport);

        // 计算时间进度和落实情况
        calculateProgressAndImplementation(projectReport);

        return dao.addProjectReport(projectReport);
    }


    /**
     * 计算时间进度和落实情况
     * @param projectReport 项目报告
     */
    private void calculateProgressAndImplementation(ProjectReport projectReport) {
        // 计算时间进度
        Date now = new Date();
        if (projectReport.getProjectBeginTime() != null && projectReport.getProjectEndTime() != null) {
            if (now.before(projectReport.getProjectBeginTime())) {
                projectReport.setTimeProgress(0.0);
            } else if (now.after(projectReport.getProjectEndTime())) {
                projectReport.setTimeProgress(100.0);
            } else {
                long total = projectReport.getProjectEndTime().getTime() - projectReport.getProjectBeginTime().getTime();
                long elapsed = now.getTime() - projectReport.getProjectBeginTime().getTime();
                double progress = (double) elapsed / total * 100;
                projectReport.setTimeProgress(progress);
            }
        } else {
            projectReport.setTimeProgress(0.0);
        }

        // 计算落实情况
        double progressDiff = projectReport.getFinishProgress() - projectReport.getTimeProgress();
        if (progressDiff > 5) {
            projectReport.setTaskImplementation("超前");
        } else if (progressDiff < -5) {
            projectReport.setTaskImplementation("落后");
        } else {
            projectReport.setTaskImplementation("正常");
        }
    }
}
